home *** CD-ROM | disk | FTP | other *** search
/ Loadstar 128 #31 / q31.d81 / t.128 rle < prev    next >
Encoding:
Text File  |  1992-01-01  |  14.6 KB  |  376 lines

  1.  
  2.  
  3.                                1 2 8    R L E
  4.  
  5.                           Program by Bob Markland
  6.  
  7.                     Text by Bob Markland and Jeff Jones
  8.  
  9.  
  10.     For several months now RLE file compression has been used to cram up to
  11. 35% more programs and text onto each issue of LOADSTAR 64.  To accomplish
  12. this feat, Jeff Jones wrote a RLE -- Run Length Encoding -- utility,
  13. published on LS #134.
  14.  
  15.     Recently, Fender sent me Jeff's M/L source code along with a request to
  16. make it work on the C-128.  After some discussion, Fender and I agreed that
  17. Jeff's program did everything he wanted it to, just not in 128 mode.  So,
  18. if you happen to subscribe to both LOADSTAR 64 and LOADSTAR 128 you will
  19. find that 128 RLE works much the same as the C-64 version.  (I have a real
  20. aversion to re-inventing the wheel).
  21.  
  22.     It will pay you, though, to become familiar with the following
  23. information because BANK selection and differences in syntax are important
  24. considerations on the C-128.
  25.  
  26.     RLE is probably the most simple form of data compression next to
  27. tokenization.  It encodes repetitious data in only two bytes instead of up
  28. to 128.  RLE uses 4 bytes to encode 256 repeating bytes.  In the code, the
  29. first byte says how many consecutive bytes to expect.  The second byte is
  30. the actual byte that's being repeated.  So 500 consecutive zeroes are coded
  31. as 8 bytes instead of 500.
  32.  
  33.     We could code up to 255 bytes, but it's more efficient to use only the
  34. lower 128 numbers for repeating characters and the higher 127 for non-
  35. repeating data.  That way we don't have to have terminating flags, which
  36. could possibly take up a lot of room if packing a big file.  Thus, we have
  37. a method by which the same byte that would flag repeating characters can be
  38. used to flag an upcoming stream of non-repeating data.  Streams of data
  39. that don't repeat are preceded by a byte that is 128 plus the number of
  40. non-repeating characters.  The machine language easily spots literals and
  41. then subtracts 128 from the code.
  42.  
  43.     RLE works extremely well with fonts and most hi-res graphics.  You
  44. wouldn't want to use RLE on text unless it was full of consecutive spaces
  45. or other repeating characters.
  46.  
  47.     When used in conjunction with a screen switcher, RLE also works well
  48. with program and game screens because, except for text screens like the one
  49. you're reading now, most screens are full of consecutive spaces.  Otherwise
  50. they'd look too crowded.  So most lend themselves to RLE.  And even if the
  51. screen doesn't pack well, the color memory of a text screen, even if it
  52. varies from line to line, is usually laid out in such a way that the 1000
  53. color bytes would pack down to only a few bytes.  (See "Jeff's Tutorial on
  54. Packing Screens" below).
  55.  
  56.     Heavily dithered, "misty" and "busy" graphics, such as the type created
  57. by Walt Harned, won't pack as well with RLE as they would with other forms
  58. of compression, though they usually pack somewhat.   Basically, the more
  59. you see solid, unobstructed blocks of pixels or color, the more packable
  60. that graphic is.
  61.  
  62.  
  63.  USING 128/RLE
  64.  -------------
  65.  
  66.     There are three versions of 128/RLE on the disk:
  67.  
  68.       FILENAME      SYS LOCATION
  69.       --------------------------
  70.       128/RLE 0C00     3072
  71.       128/RLE 1300     4864
  72.       128/RLE 1C00     7168
  73.  
  74.  NOTE: The source code to the routine (in EBUD format) is on the disk in
  75. the file, "128/rle.s".
  76.  
  77.     Load any one of these files into BANK 0, using:
  78.  
  79.   BLOAD"128/RLE xxxx",Un,B0,Pnnnn
  80.  
  81.     Which version above is the best one for you to use in a program?  That
  82. depends on the other ML programs or routines that you might want to use.
  83. The first two ($0C00 and $1300) can be used as long as you aren't using
  84. that area for other ML routines.  The code at $1C00 requires that you open
  85. up the graphic area ($1C00-$4000) with a GRAPHIC1 command.
  86.  
  87.  FENDER'S NOTE: Since I program only in the 80-column mode on 128
  88. computers, and invariably use CONTROL80, my programs always have
  89. BASIC moved up to $4000 with a GRAPHIC1 command.  This gives me the area
  90. from $1C00 to $4000 for storing screens, data, or even machine language
  91. routines like 128 REL.
  92.  
  93.     In all the examples 128/RLE observes the following conventions. You
  94. will note that they closely parallel BASIC 7.0 commands such as BLOAD and
  95. BSAVE, with two major exceptions:
  96.  
  97.  (1) You MUST follow the SYS command with a colon (:) rather than a comma.
  98.  
  99.  (2) Unlike BASIC 7.0 there are no default values.  You MUST supply ALL
  100. drive (U), bank (B), and address (P) information, as shown in the examples
  101. below.
  102.  
  103.  SYSADDR[+N]:A,B$,C
  104.  
  105.     ADDR[+N]: is the LOAD address of the M/L module plus N if applicable.
  106. A, B$ and C are parameters.  Parameters followed by $, such as B$, can be
  107. supplied in string variables or literals within quotes or a combination of
  108. the two, or any algorithm that creates a string.  Numeric parameters can be
  109. replaced with variables.  NO QUOTES!!!  You may also use any formula or
  110. equation that generates a positive integer.
  111.  
  112.  IMPORTANT NOTE:  If you choose to use string variables, a combination of
  113. string and literal text in quotes, or numeric variables, they MUST be
  114. enclosed in parentheses ().
  115.  
  116.  
  117.  PACKING A FILE
  118.  --------------
  119.  
  120.  SYSADDR+3:FILE$,U1,B1,P1:DEST$,U2,P2
  121.  
  122.     This command will BLOAD a file to a buffer which you specify and pack
  123. it, then SAVE the packed file under a new filename on any active drive,
  124. with any LOAD address you desire.
  125.  
  126.   ADDR+3 is the location of the pack file code.
  127.  
  128.   FILE$ is the parameter where you plug in the filename.  Since this isn't
  129. an archive utility, and the packed files are meant to be used in memory,
  130. the file must be small enough to fit into memory.
  131.  
  132.   U1 is the device number of the source drive.
  133.  
  134.   B1 is the bank where the source file will be loaded.
  135.  
  136.   P1 is the area where the file will be examined by 128/RLE.  When you pack
  137. from disk file to disk file you will no doubt use the "RLE DISK TO DISK"
  138. BASIC program included on this disk (and explained later) or your own
  139. embellished version, which will probably be very short.  So you can select
  140. a buffer at any address in BANK 0, following the BASIC program.
  141.  
  142.   This can be as low as 8192, which gives you room for a buffer of 223
  143. blocks ($2000-$FEFF).  Address 16384+ ($4000+) in BANK 1 is used as the
  144. default output buffer.  However, you can select any area of BANK 1, if you
  145. find a need, by entering:
  146.  
  147.   POKESYS+18,page.
  148.  
  149.   For example: POKESYS+18,32 lowers the beginning of the buffer to 8192 in
  150. BANK 1.  If you find BANKing a little confusing, note that a graphic file
  151. being examined at 8192 in BANK 0 and the output buffer at 8192 in BANK 1 do
  152. not interfere with one another.  You can even select a buffer directly
  153. under 128/RLE.
  154.  
  155.   DEST$ is the filename of your destination file.
  156.  
  157.   U2 is the device number of your destination drive.
  158.  
  159.   P2 is the desired LOAD address of the packed file.
  160.  
  161.  
  162.  UNPACKING FILES
  163.  ---------------
  164.  
  165.     Files can be unpacked in one of two ways.  Packed files can be BLOADED
  166. into a buffer by a routine in your program and then unpacked from one
  167. location to another or they can be unpacked straight from disk with the
  168. following command:
  169.  
  170.  SYSADDR:FILE$,Un,Bn,Pn
  171.  
  172.    FILE$ is the filename of the packed file.
  173.  
  174.    Un is the device number of the drive containing the file you wish to
  175. unpack.
  176.  
  177.    Bn is the bank number to which the file is to be loaded (usually 1 or
  178. 0).
  179.  
  180.    Pn is the desired LOAD address of the data.  Making this parameter 0
  181. will cause the unpack routine to respect the LOAD address the file was
  182. created with.
  183.  
  184.  
  185.  COMPRESS RANGE OF MEMORY
  186.  ------------------------
  187.  
  188.  SYSADDR+6:B1,P1,P2,B2,P3
  189.  
  190.     Here you can compress any range of memory and stash the compressed data
  191. anywhere in RAM.  This is useful if you already have data in memory which
  192. you want to compress.  Locations 253 and 254 or F% will report the end of
  193. the output buffer, which you will need to know if you want to BSAVE the
  194. data.  If F% is a negative number (integer variables only go up to 32768),
  195. you can derive the end from PEEK(254)*256+PEEK(253).
  196.  
  197.   B1 is the bank where the decompressed data resides.
  198.  
  199.   P1 is the beginning address of the decompressed data.
  200.  
  201.   P2 is the ending address of the data to be packed PLUS 1 (+1).
  202.  
  203.   B2 is the bank where the packed data is to be placed.
  204.  
  205.   P3 is the beginning address where the packed data is to be placed.
  206.  
  207.     Remember that data in corresponding areas of BANK 0 and BANK 1 will not
  208. cause conflicts.
  209.  
  210.  
  211.  DECOMPRESS DATA IN MEMORY
  212.  -------------------------
  213.  
  214.  SYSADDR+9:B1,P1,B2,P2
  215.  
  216.     Here you can decompress data already in memory and write it to any
  217. location in RAM.
  218.  
  219.   B1 is the number of the bank containing compressed data.
  220.  
  221.   P1 is the address of the compressed data.
  222.  
  223.   B2 is the number of the bank where you want the decompressed data to be
  224. written.
  225.  
  226.   P2 is the address where you want the decompressed data to be written.
  227.  
  228.  
  229.  PACK RANGE OF MEMORY
  230.  --------------------
  231.  
  232.  SYSADDR+12
  233.  
  234.     This is the same as "Compress Range of Memory," except that you must
  235. first POKE the desired values into memory then SYS directly to the M/L
  236. without parameters.
  237.  
  238.   POKE176,n - Bank containing decompressed data.
  239.  
  240.   POKE251,n:POKE252,n - Low byte/high byte for the beginning address of the
  241. decompressed data.
  242.  
  243.   POKE174,n:POKE175,n - Low byte/high byte of the end of the decompressed
  244. data PLUS 1.
  245.  
  246.   POKE177,n - Bank to which the compressed data is to be written.
  247.  
  248.   POKE253,n:POKE254,n - Low byte/high byte for the address to which the
  249. compressed data is to be written.
  250.  
  251.     As above, locations 253 and 254 or F% will report the end of the output
  252. buffer.
  253.  
  254.  
  255.  DECODE RANGE OF MEMORY
  256.  ----------------------
  257.  
  258.  SYSADDR+15
  259.  
  260.     This is the same as "Decompress Range of Memory," except that you must
  261. first POKE the desired values into memory then SYS directly to the M/L
  262. without parameters.
  263.  
  264.   POKE176,n - Bank containing compressed data.
  265.  
  266.   POKE251,n:POKE252,n - Low byte/high byte for the beginning address of the
  267. compressed data.
  268.  
  269.   POKE177,n - Bank to which the decompressed data is to be written.
  270.  
  271.   POKE253,n:POKE254,n - Low byte/high byte for the address to which the
  272. decompressed data is to be written.
  273.  
  274.     Although there may be times when "Pack Range of Memory" and "Decode
  275. Range of Memory" will be of use to the BASIC programmer they will be most
  276. valuable to the M/L programmer.
  277.  
  278.     STA, STX or STY values to the required registers as is convenient. Then
  279. JSR ADDR+12 or JSR ADDR+15 to accomplish compression or decompression.
  280.  
  281.  
  282.  MEMORY MANAGEMENT
  283.  -----------------
  284.  
  285.     There is a great deal more RAM available in the C-128 than in the C-64,
  286. so memory management is seldom a problem.  All you really need to know is:
  287. BASIC programs reside in BANK 0 anywhere from 7168 ($1C00) to 65279
  288. ($FEFF).  BASIC variables are written upward in BANK 1 from 1024 ($0400),
  289. and BASIC strings are written downward in BANK 1 from 65279 ($FEFF).  This
  290. means that any available BANK 0 space following your BASIC program, and
  291. much of the area in the middle of BANK 1 is available to manipulate data.
  292. Just how much of BANK 1 remains available will be dependent upon how many
  293. variables, strings and arrays your program uses.
  294.  
  295.  
  296.  PACK RLE TO DISK PROGRAM
  297.  ------------------------
  298.  
  299.     This BASIC program is an ultra Q&D utility.  In fact, it is only two
  300. lines long.  But, that's all that's necessary to compress a disk file to
  301. the same or another disk.
  302.  
  303.     To use this utility, type RUN"RLE DISK TO DISK" and press RETURN.
  304.  
  305.     The program will BLOAD 128/RLE, LIST line 100, print GOTO100, then
  306. STOP.  Move the cursor up to line 100 and proceed as follows:
  307.  
  308.   Change the first half of the line by substituting the appropriate source
  309. filename and source drive. There should seldom if ever be a reason to alter
  310. the B0 or P12288.
  311.  
  312.   Continue in the second half of the line, following the colon (:) by
  313. substituting the appropriate destination filename, destination drive, and
  314. desired load address for the packed file.
  315.  
  316.   To avoid later confusion, it is best give the destination filename an
  317. RLE. prefix, however it is not mandatory.  Enter P0 if you wish 128/RLE to
  318. respect the load address of the source file.
  319.  
  320.   When you are satisfied that line 100 is correct, make certain that the
  321. source disk and destination disk (if applicable) are in the drive(s), move
  322. the cursor down to GOTO100 and press RETURN.
  323.  
  324.     If you are compressing numerous files you can go back to line 100 and
  325. proceed as indicated above.  Or you may prefer to write your own version
  326. and embellish this utility to suit your own needs.
  327.  
  328.     With 128/RLE's ability to process string and numeric variables as its
  329. parameters it would be a simple matter to incorporate keyboard input or an
  330. array to process multiple-file compression projects.  As written, 128/RLE
  331. doesn't scratch an existing destination file so you must specify a
  332. different filename if that filename exists on the destination disk.
  333.  
  334.     As you contemplate uses for 128/RLE it may be helpful to know that
  335. although the C-64 and C-128 each need their own RLE program -- graphic,
  336. font and 40 column screen files created by either are fully
  337. interchangeable.
  338.  
  339.     Perhaps one of the most obvious uses for RLE packing is a graphics
  340. slide-show.  But, whenever you find a need to conserve disk space 128/RLE
  341. is the answer.
  342.  
  343.     In the future, compression will become an even larger part of the
  344. computer industry, incorporated into the very chips of every computer.
  345.  
  346.  
  347.  JEFF'S TUTORIAL ON PACKING SCREENS
  348.  ----------------------------------
  349.  
  350.     You'll find that this M/L won't pack screens as they are stored in
  351. memory.  Screens and color memory are two separate chunks of memory. Screen
  352. memory is fine, but color memory, since it is part of hardware, and not
  353. RAM, is kind of tricky. This is because the M/L always reads and stores
  354. BENEATH the I/O image at $D000-$DFFF.  Color memory is at $D800 for the 40-
  355. column screen.  So the packing algorithm will read beneath the color
  356. nibbles to the RAM bytes in the same location, and the unpacker will always
  357. stash color beneath $D800, invisible to color. This becomes even more
  358. complicated in the 80 column mode where screen and color RAM must be
  359. written to indirectly.
  360.  
  361.     The way to pack screens is to first stash a screen anywhere with a
  362. toolbox, or your own M/L.  Choose this place as your throwaway docking
  363. station for screens.  You're going to immediately pack it and move it
  364. someplace else.  Your stored screen will take up an 8-page range of memory.
  365. Next, pack the range of memory with 128/RLE.  Locations 253 and 254 will
  366. report the end of the stored packed screen.  There plus 1 is where you can
  367. store the next screen.
  368.  
  369.     Before you can re-display a packed screen, you must unpack it to a
  370. location where your screen switcher can then restore the screen.  Since
  371. screens will be of variable length, you will have to come up with a
  372. workable management routine if you will be storing many, many screens.
  373.  
  374.                           \\\\\ RETURN - Menu \\\\\
  375.  
  376.